import 'package:flutter/material.dart';

class ArrowPainter extends CustomPainter {
  final Color? arrowColor;

  const ArrowPainter({
    this.arrowColor,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    paint.color = arrowColor ?? Colors.white38;

    final path = _createPath(
      firstBlockHeight: size.height * 0.3,
      firstBlockWidth: size.width * 0.05,
      thirdBlockHeight: size.height * 0.3,
      thirdBlockWidth: size.width * 0.2,
      triangleSize: size.height * 0.3,
      offsetBetween: size.width * 0.05,
      width: size.width,
      centerY: size.height / 2.0,
    );

    canvas.drawPath(path, paint);
  }

  Path _createPath({
    required double firstBlockHeight,
    required double firstBlockWidth,
    double? secondBlockHeight,
    double? secondBlockWidth,
    required double thirdBlockHeight,
    required double thirdBlockWidth,
    required double triangleSize,
    required double offsetBetween,
    required double width,
    required double centerY,
  }) {
    secondBlockHeight ??= firstBlockHeight;
    secondBlockWidth ??= firstBlockWidth;

    final path = Path();

    final arrowWidth = firstBlockWidth +
        secondBlockWidth +
        thirdBlockWidth +
        triangleSize +
        offsetBetween +
        offsetBetween;

    var offset = width / 2.0 - arrowWidth / 2.0;
    final rect = Rect.fromLTWH(
      offset,
      centerY - firstBlockHeight / 2.0,
      firstBlockWidth,
      firstBlockHeight,
    );
    path.addRect(rect);
    offset = offset + firstBlockWidth + offsetBetween;

    final rect2 = Rect.fromLTWH(
      offset,
      centerY - secondBlockHeight / 2.0,
      secondBlockWidth,
      secondBlockHeight,
    );
    path.addRect(rect2);

    offset = offset + secondBlockWidth + offsetBetween;

    final rect3 = Rect.fromLTWH(
      offset,
      centerY - thirdBlockHeight / 2.0,
      thirdBlockWidth,
      thirdBlockHeight,
    );
    path.addRect(rect3);

    offset = offset + thirdBlockWidth;

    path.addPath(
      getTrianglePath(
        Offset(
          offset,
          centerY,
        ),
        triangleSize,
      ),
      Offset.zero,
    );

    return path;
  }

  Path getTrianglePath(Offset offset, double size) {
    return Path()
      ..moveTo(offset.dx, offset.dy - size)
      ..lineTo(offset.dx + size, offset.dy)
      ..lineTo(offset.dx, offset.dy + size);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
